package com.study.edoc.controller;


import com.study.edoc.common.Pageable;
import com.study.edoc.exception.BusinessException;
import com.study.edoc.exception.R;
import com.study.edoc.exception.ResultCodeEnum;
import com.study.edoc.pojo.Document;
import com.study.edoc.service.DocumentService;
import com.study.edoc.vo.DocumentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/api/v1/document")
@Api(tags = "电子文档模块",description = "电子文档模块相关接口")
public class DocumentController {

    @Resource
    private DocumentService documentService;


    @ApiOperation(value = "根据文档类别分页查询电子文档列表")
    /*
    * paramType参数值包含如下：
        header->请求参数的获取：@RequestHeader(代码中接收注解)
        query->请求参数的获取：@RequestParam(代码中接收注解)
        path->（用于restful接口）–>请求参数的获取：@PathVariable(代码中接收注解)
        body->请求参数的获取：@RequestBody(代码中接收注解)
    * */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path",name = "cid",value = "文档分类编号",dataType = "int",defaultValue = "0"),
            @ApiImplicitParam(paramType = "path",name = "page",value = "当前页索引",dataType = "int",defaultValue = "1"),
            @ApiImplicitParam(paramType = "path",name = "limit",value = "每页显示多少条记录",dataType = "int",defaultValue = "2")
    })
    @GetMapping("/search")
    public Map<String,Object> listByPage(
            @RequestParam(value = "cid",required = false,defaultValue = "0") int cid,
            @RequestParam(value = "page",required = true,defaultValue = "1") int page,
            @RequestParam(value = "limit",required = false,defaultValue = "2") int limit){
        Pageable<DocumentVO> pageable = documentService.findDocsByPage(cid, page, limit);
        Map<String,Object> jsonMap = new ConcurrentHashMap<>();
        jsonMap.put("code",0);
        jsonMap.put("msg","");
        jsonMap.put("count",pageable.getTotal());
        jsonMap.put("data",pageable.getData());
        return jsonMap;
    }



    @PostMapping("/save")
    @ApiOperation(value = "保存电子文档信息")
    @ApiImplicitParam(paramType = "body",name = "doc",value = "电子文档对象实体",dataType = "Document")
    public R saveDoc(@RequestBody Document doc){
        int retVal = documentService.addDoc(doc);
        if(retVal > 0){
            return R.success(0,"保存电子文档成功");
        }
        else{
            return R.fail("保存电子文档失败");
        }
    }


    @ApiOperation(value = "更新电子文档信息")
    @ApiImplicitParam(paramType = "body",name = "doc",value = "电子文档对象实体",dataType = "Document")
    @PutMapping("/update")
    public R updateDoc(@RequestBody Document doc){
        int retVal = documentService.modifyDoc(doc);
        if(retVal > 0){
            return R.success(0,"更新电子文档成功");
        }
        else{
            return R.fail("更新电子文档失败");
        }
    }


    @ApiOperation(value = "根据指定的电子文档编号删除电子文档条目")
    @ApiImplicitParam(name = "id", value = "电子文档编号", dataType = "int", paramType = "path")
    @PostMapping("/delete/{id}")
    public R deleteDocById(@PathVariable("id") int id){
        int retVal = documentService.deleteDocById(id);
        if(retVal > 0){
            return R.success(0,"删除电子文档成功");
        }
        else{
            return R.fail("删除电子文档失败");
        }
    }


}
